//
//  CRMFollowHistoryCell.m
//  New_Runway
//
//  Created by NewLife on 2018/3/9.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CRMFollowHistoryCell.h"
#import "CRMFollowHistoryModel.h"

@implementation CRMFollowHistoryCell
{
    UIImageView *_stateImageView;
    UILabel *_timeLabel;
    UILabel *_remarkLabel;
    UILabel *_followLabel;
    UILabel *_levelLabel;
    
    UIView *_lineView;
    
    UIImageView *_carImageView;
    UILabel *_carNameLabel;
    UILabel *_carPricelabel;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.separatorInset = UIEdgeInsetsMake(0, 56, 0, 0);
        
        [self setupUI];
    }
    return self;
}

- (void)setModel:(CRMFollowHistoryResultModel *)model {
    
    _model = model;
    
    _carImageView.hidden = YES;
    _carNameLabel.hidden = YES;
    _carPricelabel.hidden = YES;

    if (model.followState == 10) {//跟进
        _stateImageView.image = getImageFromName(@"crm_genjin");
        _timeLabel.text = [NSString stringWithFormat:@"跟进时间: %@",model.nextFollowTime];
    } else if (model.followState == 20) {//已预约
        _stateImageView.image = getImageFromName(@"crm_yuyue");
        _timeLabel.text = [NSString stringWithFormat:@"预约时间: %@",model.appointmentTime];
    } else if (model.followState == 30) {//已战败
        _stateImageView.image = getImageFromName(@"crm_zhanbai");
        _timeLabel.text = [NSString stringWithFormat:@"战败时间: %@",model.loseEfficacyTime];
    } else if (model.followState == 40) {//已失效
        _stateImageView.image = getImageFromName(@"crm_shixiao");
        _timeLabel.text = [NSString stringWithFormat:@"失效时间: %@",model.loseEfficacyTime];
    } else if (model.followState == 50) {//已成交
        _stateImageView.image = getImageFromName(@"crm_chengjiao");
        _timeLabel.text = [NSString stringWithFormat:@"成交时间: %@",model.fixtureTime];
        _carImageView.hidden = NO;
        _carNameLabel.hidden = NO;
        _carPricelabel.hidden = NO;
    }
    
    if (_timeLabel.text.length >= 16) {
        _timeLabel.text = [_timeLabel.text substringToIndex:16];
    }
    _remarkLabel.text = isEmptyOrNilString(model.followRemark) == YES? @"备注: ": [NSString stringWithFormat:@"备注: %@",model.followRemark];
    _followLabel.text = model.followName;
    _levelLabel.text = model.customerLevelTxt;
    [_carImageView sd_setImageWithURL:[NSURL URLWithString:model.fixtureCarPic] placeholderImage:getImageFromName(@"tu")];
    _carNameLabel.text = model.fixtureCar;
    _carPricelabel.text = [NSString stringWithFormat:@"¥%.2f万",model.fixturePrice.floatValue];
    
    [self layoutUI];
}

- (void)setShowLineView:(BOOL)showLineView {
    
    _showLineView = showLineView;
    
    _lineView.hidden = !showLineView;
}

- (void)setupUI {
    
    _stateImageView = DPImageView()
    .setBackgroundColor([UIColor whiteColor]);
    
    _timeLabel = DPLabel()
    .setFont(kPingFangRegular10())
    .setTextColor(getColorFromString(@"989898"))
    .setBackgroundColor([UIColor whiteColor]);
    
    _remarkLabel = DPLabel()
    .setFont(kPingFangRegular12())
    .setTextColor(getColorFromString(@"323232"))
    .setBackgroundColor([UIColor whiteColor]);
    
    _followLabel = DPLabel()
    .setFont(kPingFangRegular12())
    .setTextColor(getColorFromString(@"989898"))
    .setBackgroundColor([UIColor whiteColor]);
    
    _levelLabel = DPLabel()
    .setFont(kPingFangRegular12())
    .setTextColor(getColorFromString(@"989898"))
    .setBackgroundColor([UIColor whiteColor]);
    
    _lineView = DPView().setBackgroundColor(getColorFromString(@"e1e1e1"));
    
    _carImageView = DPImageView().setBackgroundColor([UIColor whiteColor]);
    _carNameLabel = DPLabel()
    .setFont(kPingFangRegular12())
    .setTextColor(getColorFromString(@"323232"));
    _carNameLabel.numberOfLines = 2;
    
    _carPricelabel = DPLabel()
    .setFont(kPingFangRegular13())
    .setTextColor(getColorFromString(@"ff6816"));
    
    self.contentView
    .addSubview(_stateImageView)
    .addSubview(_timeLabel)
    .addSubview(_remarkLabel)
    .addSubview(_followLabel)
    .addSubview(_levelLabel)
    .addSubview(_lineView)
    .addSubview(_carImageView)
    .addSubview(_carNameLabel)
    .addSubview(_carPricelabel);
}

- (void)layoutUI {
    
    _stateImageView.frame = CGRectMake(16, 0, 31, 31);
    
    [_timeLabel sizeToFit];
    _timeLabel.frame = CGRectMake(_stateImageView.right + 12, 10, _timeLabel.width, _timeLabel.height);
    
    _remarkLabel.frame = CGRectMake(_timeLabel.left, _timeLabel.bottom + 6, Screen_width - 56 - 25, 19);
    
    if (self.model.followState == 50) {
        _carImageView.frame = CGRectMake(_remarkLabel.left, _remarkLabel.bottom + 6, 90, 60);
        _carNameLabel.frame = CGRectMake(_carImageView.right + 10, _carImageView.top + 3, Screen_width - 156 - 25, 36);
        _carPricelabel.frame = CGRectMake(_carNameLabel.left, _carImageView.bottom - 3 - _carPricelabel.height, _carNameLabel.width, 19);
        _followLabel.frame = CGRectMake(_carImageView.left, _carImageView.bottom + 9, Screen_width - 56 - 25, 19);
    } else {
        _followLabel.frame = CGRectMake(_remarkLabel.left, self.contentView.bottom - 28, Screen_width - 56 - 25, 19);
    }
    
    [_levelLabel sizeToFit];
    _levelLabel.frame = CGRectMake(Screen_width - 17 - _levelLabel.width, 10, _levelLabel.width, _levelLabel.height);
    _lineView.frame = CGRectMake(_stateImageView.centerX - 0.25, _stateImageView.bottom, 0.5, self.height - _stateImageView.height);
}

@end
